<?php

namespace app\webapi\controller;

use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use think\response\Json;

class ApiCommon extends ApiBaseController
{
    // public $user_info = null;

    // public function __construct()
    // {
    //     $this->user_info = $this->checkToken();
    //     if (!preg_match('/^\d+$/', $this->user_info['id'])) {
    //         $this->result_error('用户id出错');
    //     }
    // }

    /** 获取商品分类【2】 */
    public function getCategory()
    {
        $data = ApiRedis::updateGetByDB(3, self::KEYS['CATEGORY_LIST'], function () {
            $sql = "SELECT *,id as cid FROM sy_goods_category WHERE state=1";
            $sql_result = Db::query($sql, []);
            return $sql_result;
        });
        $this->result_ok("ok", $data);
    }

    /** 获取公告【0】 */
    public function getNotice()
    {
        $data = ApiRedis::updateGetByDB(3, self::KEYS['NOTICE_LIST'], function () {
            $sql = "SELECT *,id as nid FROM sy_public_notice WHERE state>0 ORDER BY no,id ASC";
            $sql_result = Db::query($sql, []);
            return $sql_result;
        });
        $this->result_ok("ok", $data);
    }

    /** 获取货架商品列表【2】 */
    public function getGoodsList()
    {
        $uid = 0;
        $user_info = $this->checkToken(false);
        if(isset($user_info['state']) && $user_info['state'] > 1){
            $uid = $user_info['id'];
        }
        $cid = (int)(Request::param('cid'));
        $name = Request::param('name');
        if (!($cid > 0)) {
            $cid = 0;
        }
        $keys = APiRedisServer::selected(3)->HKEYS(self::KEYS['SHELVES_GOODS'] . $cid);
        if (empty($keys)) {
            $this->result_ok("ok", []);
        }
        $data = APiRedisServer::selected(3)->HMGET(self::KEYS['SHELVES_GOODS'] . $cid, $keys);
        foreach ($data as &$v) {
            $v = json_decode($v, true);
        }
        usort($data, function ($current, $next) {
            return $current['id'] - $next['id'];
        });
        if (!empty($name)) {
            $data = array_filter($data, function ($item) use ($name) {
                return strpos($item, $name);
            });
        }
        $this->result_ok("ok", $data);
    }

    /** 获取Banner列表【2】 */
    public function getBannerList()
    {
        $data = ApiRedis::updateGetByDB(3, self::KEYS['BANNER_LIST'], function () {
            $sql = "SELECT id as gid,code,cid,name,summary,description,price,img FROM sy_goods_shelves WHERE state=1 ORDER BY id DESC LIMIT 5";
            $sql_result = Db::query($sql, []);
            return $sql_result;
        });
        $this->result_ok("ok", $data);
    }

    /** 获取协议页面基地址【1】 */
    public function getProtocolBaseUrl()
    {
        $this->result_ok('ok', Config::get('app.app_host').'/web/common/getProtocol');
    }

    /** 获取协议【1】 */
    public function getProtocol()
    {
        $protocol = Request::param('protocol');
        $pid = 0;
        if ($protocol == "用户须知") {
            $pid = 1;
        } else if ($protocol == "用户隐私政策") {
            $pid = 2;
        } else if ($protocol == "注册及服务协议") {
            $pid = 3;
        } else if ($protocol == "商品上架规范及用户间交易协议") {
            $pid = 4;
        } else if ($protocol == "上架品类资质名录") {
            $pid = 5;
        } else if ($protocol == "促销活动协议") {
            $pid = 6;
        } else if ($protocol == "扣款协议") {
            $pid = 7;
        } else if ($protocol == "员工用语及行为标准") {
            $pid = 8;
        } else if ($protocol == "著作权转让合同") {
            $pid = 9;
        } else if ($protocol == "供货商入驻服务协议") {
            $pid = 10;
        } else if ($protocol == "供销框架协议含订单样式") {
            $pid = 11;
        } else if ($protocol == "用户推荐协议") {
            $pid = 12;
        }
        if ($pid == 0) {
            $this->result_error("加载失败");
        }

        $data = ApiRedis::updateGetByDB(3, self::KEYS['PROTOCOL_LIST'] . $pid, function () use ($pid) {
            $sql = "SELECT * FROM sy_other_protocol WHERE pid=?";
            $sql_result = Db::query($sql, [$pid]);
            if (!empty($sql_result)) {
                return $sql_result[0];
            }
            return null;
        });
        //var_dump($data);die;
        if (!empty($data)) {
            $con = isset($data['content']) ? $data['content'] : $data;
            return response($con);
        }
        return '
        <div>
            <p style="margin:20px;fpnt-size:1.5em;">页面不见了...</p>
            <Button class="backhome"
                content="返回首页"
                v-on:onClick="onClick" />
        </div> ';
    }

    /** 获取上架手续费率【2】 */
    public function getHandlingFeeRate()
    {
        $data = APiRedisServer::selected(3)->get(self::KEYS['RATE_HANHDING']);
        if (empty($rate)) {
            $sql_result = Db::name('other_config')->where('id', 1)->find();
            $rate = $sql_result['value3'];
            APiRedisServer::selected(3)->set(self::KEYS['RATE_HANHDING'], $rate);
        }
        $this->result_ok("ok", $rate);
    }

    /** 获取商品委托上架时间【2】 */
    public function getGoodsLaunchTime()
    {
        $data = APiRedisServer::selected(3)->get(self::KEYS['GOODS_LAUNCH_TIME']);
        if (empty($rate)) {
            $sql_result = Db::name('other_config')->where('id', 6)->find();
            $rate = $sql_result['value1'];
            APiRedisServer::selected(3)->set(self::KEYS['GOODS_LAUNCH_TIME'], $rate);
        }
        $this->result_ok("ok", $rate);
    }

    /** 获取全平台商品上架功能状态【2】【2】 */
    public function getAllGoodsLaunchStatus()
    {
        $data = APiRedisServer::selected(3)->get(self::KEYS['ALL_GOODS_LAUNCH_STATE']);
        if (empty($rate)) {
            $sql_result = Db::name('other_config')->where('id', 7)->find();
            $rate = $sql_result['value1'];
            APiRedisServer::selected(3)->set(self::KEYS['ALL_GOODS_LAUNCH_STATE'], $rate);
        }
        $this->result_ok("ok", $rate);
    }

    /** 获取商品上架涨幅【2】 */
    public function getGoodsLaunchGain()
    {
        $data = APiRedisServer::selected(3)->get(self::KEYS['ALL_GOODS_LAUNCH_STATE']);
        if (empty($rate)) {
            $sql_result = Db::name('other_config')->where('id', 8)->find();
            $rate = $sql_result['value2'];
            APiRedisServer::selected(3)->set(self::KEYS['ALL_GOODS_LAUNCH_STATE'], $rate);
        }
        $this->result_ok("ok", $rate);
    }

    /** 获取商品上架涨幅【2】 */
    public function setData()
    {
        $type = Request::param('type');
        $sid = Request::param('sid');
        $user_info = $this->checkToken();
        $uid = $user_info['id'];
        switch ($type) {
            case '进入场次':
                $sid = (int)$sid;
                // // 计算Timeout: 当前晚上12点过期
                // let curTime = new Date();
                // let y = curTime.getFullYear();
                // let M = curTime.getMonth();
                // let d = curTime.getDate();
                // let expiresTime = new Date(y, M, d + 1, 0, 0, 0);
                // let timeout = Math.floor((expiresTime.getTime() - curTime.getTime()) / 1000);
                $data = APiRedisServer::selected(3)->hGet(self::KEYS['LOG_SCHEDULE'] . $sid, $uid);
                if ($data) {
                    APiRedisServer::selected(3)->hIncrBy(self::KEYS['LOG_SCHEDULE'] . $sid, $uid, 1);
                } else {
                    APiRedisServer::selected(3)->hSet(self::KEYS['LOG_SCHEDULE'] . $sid, $uid, '1');
                }
                break;
            default:
                break;
        }
        $this->result_ok("ok");
    }

    public function test()
    {
        $s = Request::param('s');
        $data = [];
        $alldata = APiRedisServer::selected(3)->hKeys(self::KEYS['LOG_SCHEDULE'] . $s);
        if (!empty($alldata)) {
//        $last = alldata.map(i => {
//            return parseInt(i);
//        })
            $r = Db::query("SELECT uid,nickname,phone FROM user WHERE uid IN (?)", [$alldata]);
            $data = $r;
        }
        return response($data);
    }


}